package com.wenhuitec.android.tang.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.dhkj.web.bean.AddrTree;
import com.dhkj.web.bean.OrderProductModel;
import com.dhkj.web.utils.MyObserverRes;
import com.dhkj.web.utils.RequestUtils;
import com.wenhuitec.android.tang.R;
import com.wenhuitec.android.tang.config.SystemInit;
import com.wenhuitec.android.tang.entity.Orders;
import com.wenhuitec.android.tang.ui.adapter.OrderAdapter;
import com.wenhuitec.android.tang.ui.dialog.OrdersDialog;
import com.wenhuitec.android.tang.ui.view.OrderView;
import com.wenhuitec.android.tang.util.UsbPrinter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class ActivityCheckOrder extends AppCompatActivity {


    ArrayAdapter<String> adapter;
    ArrayAdapter<AddrTree> adapter2;
    private List<AddrTree> list = new ArrayList<>();
    private List<AddrTree> shopList = new ArrayList<>();
    private List<String> typeList = new ArrayList<>();
    private Context mContext;

    private List<AddrTree> addrTreeList;
    private RecyclerView recyclerView;
    OrderAdapter orderAdapter;
    private TextView amountPriceTv;
    private TextView amountCountTv;
    private List<OrderProductModel> productModelList = new ArrayList<>();
    private OrderView orderView;
    private UsbPrinter usbPrinter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_order);
        mContext = this;

        recyclerView = findViewById(R.id.ordersRv);
        amountPriceTv = findViewById(R.id.amountPriceTv);

        amountCountTv = findViewById(R.id.amountCountTv);
        orderView = findViewById(R.id.orderView);
        orderAdapter = new OrderAdapter(mContext, new ArrayList<>(), orders -> {
            orderView.setVisibility(View.VISIBLE);
            orderView.shows(orders);
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(orderAdapter);
        addrTreeList = SystemInit.getInstance(mContext).getAddrTreeList();
        orderView.setOrderReturn(() -> {
            init();
        });

        usbPrinter = UsbPrinter.getInstance(this);
        init();
    }

    private void init() {

        SharedPreferences sharedPreferences = getSharedPreferences("systemI", Context.MODE_PRIVATE);
        String user = sharedPreferences.getString("user", "");
        RequestUtils.order(mContext, user, new MyObserverRes<List<OrderProductModel>>(mContext) {
            @Override
            public void onSuccess(List<OrderProductModel> result) {
                List<Orders> ordersList = new ArrayList<>();
                BigDecimal count = new BigDecimal(0);
                BigDecimal price = new BigDecimal(0);
                for (OrderProductModel goo : result) {
                    Orders orders = new Orders();
                    orders.setOrders(goo.getDh());
                    orders.setNumber(new BigDecimal(goo.getAmountCount()));
                    orders.setPrice(new BigDecimal(goo.getAmountPrice()));
                    orders.setOrderProductModel(goo);
                    orders.setCardno(goo.getCardno());
                    ordersList.add(orders);
                    count = count.add(orders.getNumber());
                    price = price.add(orders.getPrice());
                    orders.setOrdersTime(goo.getInDate());
                    orderAdapter.setOrdersList(ordersList);
                }
                amountCountTv.setText("X" + count.stripTrailingZeros().toPlainString());
                amountPriceTv.setText("￥" + price.stripTrailingZeros().setScale(2).toPlainString());
                orderView.setVisibility(View.GONE);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {

            }
        });

    }


}
